// pages/admin4TeacherPage/stuRepeatDetail/stuRepeatDetail.js
var app = getApp();
import commonAPI from '../../../api/api'
import util from '../../../utils/util.js';
const FN = require('../../../utils/public');
import baAPI from '../../../api/beforehandappintmentAPI'
const voice = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {},
    priTmplIdArr:[],
    vrlist: [],
    page: 1,
    vrnodealNum: 0
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.hideShareMenu();
    that.setData({
      options: options
    })
    this.getSubscribeMsg();
  },

// 获取订阅消息 
getSubscribeMsg: function () {
  var that = this;
  commonAPI.getSubscribeMsg({ pageUrl: util.getCurrentPageUrl() }).then(res => {
    if (res.code == '200' && res.data.length > 0) {
      var arr = res.data[0].priTmplIdArr.split(",")
      that.setData({
        priTmplIdArr: arr
      })
    }
  })
},

// 初始化数据
  initData: function () {
    var that = this;
    commonAPI.getSubscriber({  "pageNum": that.data.page,
    "pageSize":30}).then(res => {
      if (res.code == '200') {
        var tmp = that.data.vrlist;
      res.data.list.map(function (item, i, arr) {
        item.voiceResultInfo = JSON.parse(item.voiceResultInfo);
        if (isNaN(item.voiceResultAccuracy)) {
          item.voiceResultAccuracy = 0;
        }
        tmp.push(item);
      })
      that.setData({
        vrlist: tmp,
        vrnodealNum: res.data.total
      })
      }
    })
  },

  // 播放声音
  playVoice: function (e) {
    var i = e.currentTarget.dataset.index;
    voice.stop();
    voice.src = this.data.vrlist[i].voiceResultInfo.tempFilePath;
    voice.play();
    voice.onPlay(function () {
      wx.showLoading({
        title: '播放中',
      })
    });

    voice.onStop(function () {
      wx.hideLoading();
    });

    // 音频结束回调
    voice.onEnded(function () {
      wx.hideLoading();
    });
  },

  //忽略
  ignore: function (e) {
    var i = e.currentTarget.dataset.index, that = this;
    that.subscribeMessage();
    var vrId = that.data.vrlist[i].voiceResultId;
    var baid = that.data.vrlist[i].hasOwnProperty('beforehandAppointmentId') ? that.data.vrlist[i].beforehandAppointmentId : '';
    baAPI.ignore({ voiceResultId: vrId,
      beforehandAppointmentId: baid }).then(res => {
        if(res.data==1){
          var tmparr = that.data.vrlist;
          tmparr.splice(i,1);
          that.setData({
            vrlist:tmparr
          })
        }
    })
  },

  // 学员回复提醒
  subscribeMessage: function () {
    var that = this;
    wx.requestSubscribeMessage({
      tmplIds: that.data.priTmplIdArr,
      success(res) {
        console.log('授权成功！');
      }
    })
  },

  /**
   * 去纠音
   * 优化：此处是用户主动申请纠音的，代表已经消耗积分，老师直接纠音就行，老师不能设置是否需要消耗积分。
   * 
   */
  goCorrect: function (e) {
    var i = e.currentTarget.dataset.index, that = this;
    that.subscribeMessage();
    var vrId = that.data.vrlist[i].voiceResultId;
    wx.navigateTo({
      url: '../teaReplyStucorrect/teaReplyStucorrect?vrId=' + vrId+'&freeCorrectMode=false'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page:1,
      vrlist:[]
    })
    this.initData();    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this, i = that.data.page;
    i++;
    this.setData({
      page: i
    })
    this.initData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})